package com.mdc.mansion.core.modules.report.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.DateUtils;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.modules.order.RestaurantOrderService;
import com.mdc.mansion.core.modules.report.RestaurantReportService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;

@Service
public class RestaurantReportServiceImpl implements RestaurantReportService {

	@Autowired
	RestaurantOrderService restaurantOrderService;
	/**
	 * 查询营收额报表 
	 * 查询字段:时间(YYYY-MM-DD)，销售总金额,优惠金额,实收金额,服务费总金额,退款金额
	 *  
	 */
	@Override
	public List<Map<String, Object>> getTotalAmountList(String reportType_Day, String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Calendar c = Calendar.getInstance();
		String startDate =DateUtils.formatShortDate(c.getTime());
		String endDate = startDate;
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		Restaurant restaurant = ContextHolder.currentRestaurant();
		if(jsonObject.containsKey("date"))
		{
			startDate = jsonObject.getString("date");
		}
		
		startDate = this.getEndDate(reportType_Day,startDate,null);
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "f3617f26b5a7461ab325b73e74c8b380");
		if(RestaurantCommon.ReportType_Year.equals(reportType_Day))
		{
			return restaurantOrderService.getTotalAmountListByYear(parameterMap);
		}else if(RestaurantCommon.ReportType_All_Year.equals(reportType_Day)){
			return restaurantOrderService.getTotalAmountListByAllYear(parameterMap);
		}else if(RestaurantCommon.ReportType_Month.equals(reportType_Day))
		{
			return restaurantOrderService.getTotalAmountListByMonth(parameterMap);
		}else
		{
			return restaurantOrderService.getTotalAmountList(parameterMap);
		}
		
	}

	/**
	 * 查询各菜品分类销售情况
	 * 统计性数据，分别统计:当天,30天,一年的数据
	 * 菜品分类统计，金额
	 */
	@Override
	public List<Map<String, Object>> getCategoryAmountList(String reportType_Day, String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Calendar c = Calendar.getInstance();
		String startDate =DateUtils.formatShortDate(c.getTime());
		String endDate = startDate;
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		Restaurant restaurant = ContextHolder.currentRestaurant();
		if(jsonObject.containsKey("date"))
		{
			startDate = jsonObject.getString("date");
		}
		
		startDate = this.getEndDate(reportType_Day,startDate,null);
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "f3617f26b5a7461ab325b73e74c8b380");
		return restaurantOrderService.getCategoryAmountList(parameterMap);
	}


	/**
	 * 查询各菜品销售情况,包括数量
	 * 统计性数据，分别统计:当天,30天,一年的数据
	 * 菜品销售金额
	 */
	@Override
	public List<Map<String, Object>> getDishesAmountList(String reportType_Day, String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Calendar c = Calendar.getInstance();
		String startDate =DateUtils.formatShortDate(c.getTime());
		String endDate = startDate;
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		Restaurant restaurant = ContextHolder.currentRestaurant();
		if(jsonObject.containsKey("date"))
		{
			startDate = jsonObject.getString("date");
		}
		
		startDate = this.getEndDate(reportType_Day,startDate,null);
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "f3617f26b5a7461ab325b73e74c8b380");
		return restaurantOrderService.getDishesAmountList(parameterMap);
}
	
	/**
	 * 根据类型加日期
	 * @param reportType_Day
	 * @param startDate
	 * @param days
	 * @return
	 */
	private String getEndDate(String reportType_Day, String startDate, String days) {
		String endDate = startDate;
		try{
			if(RestaurantCommon.ReportType_Day.equals(reportType_Day))
			{
				startDate = DateUtils.formatShortDate(DateUtils.addDays(DateUtils.parseShortDate(endDate), -7));
				
			}else if(RestaurantCommon.ReportType_Month.equals(reportType_Day))
			{
				startDate = DateUtils.formatShortDate(DateUtils.addMonths(DateUtils.parseShortDate(endDate), -1));
			}else if(RestaurantCommon.ReportType_Year.equals(reportType_Day) || RestaurantCommon.ReportType_All_Year.equals(reportType_Day) )
			{
				startDate = DateUtils.formatShortDate(DateUtils.addYears(DateUtils.parseShortDate(endDate), -1));
			}else if(StringUtils.isBlank(reportType_Day))
			{
				endDate = DateUtils.formatShortDate(DateUtils.addDays(DateUtils.parseShortDate(startDate), Integer.parseInt(days)));
			}else
			{
				return null;
			}
		}catch(Exception e)
		{
			/***报表获取异常,请重试****/
			Log.error(e.getMessage());
			throw new BusinessException(I18nUtil.getMessage("report.fail.exception"));
		}
		return startDate;
	}

	@Override
	public List<Map<String, Object>> getWaitersTotalMoney(String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		
		String startDate = "";
		String endDate = "";
		if(jsonObject.containsKey("startDate"))
		{
			startDate = jsonObject.getString("startDate");
		}
		
		if(jsonObject.containsKey("endDate"))
		{
			endDate = jsonObject.getString("endDate");
		}
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "f3617f26b5a7461ab325b73e74c8b380");
		return restaurantOrderService.getWaitersTotalMoney(parameterMap);
	}

	@Override
	public List<Map<String, Object>> getCashierTotalMoney(String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		
		String startDate = "";
		String endDate = "";
		if(jsonObject.containsKey("startDate"))
		{
			startDate = jsonObject.getString("startDate");
		}
		
		if(jsonObject.containsKey("endDate"))
		{
			endDate = jsonObject.getString("endDate");
		}
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "53392e725e3942cbb7080f81e31b8e0d");
		return restaurantOrderService.getCashierTotalMoney(parameterMap);
	}

	@Override
	public List<Map<String, Object>> getTotalDiscountMoney(String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		
		String startDate = "";
		String endDate = "";
		if(jsonObject.containsKey("startDate"))
		{
			startDate = jsonObject.getString("startDate");
		}
		
		if(jsonObject.containsKey("endDate"))
		{
			endDate = jsonObject.getString("endDate");
		}
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "53392e725e3942cbb7080f81e31b8e0d");
		return restaurantOrderService.getTotalAmountList(parameterMap);
	}

	@Override
	public List<Map<String, Object>> getCancelDishesTotalMoney(String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		
		String startDate = "";
		String endDate = "";
		if(jsonObject.containsKey("startDate"))
		{
			startDate = jsonObject.getString("startDate");
		}
		
		if(jsonObject.containsKey("endDate"))
		{
			endDate = jsonObject.getString("endDate");
		}
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "53392e725e3942cbb7080f81e31b8e0d");
		return restaurantOrderService.getCancelDishesTotalMoney(parameterMap);
	}

	@Override
	public List<Map<String, Object>> getCancelOrderTotalMoney(String data) {
		JSONObject  jsonObject  = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		
		String startDate = "";
		String endDate = "";
		if(jsonObject.containsKey("startDate"))
		{
			startDate = jsonObject.getString("startDate");
		}
		
		if(jsonObject.containsKey("endDate"))
		{
			endDate = jsonObject.getString("endDate");
		}
		
		parameterMap.put("startDate", startDate);
		parameterMap.put("endDate", endDate);
		parameterMap.put("restaurantId", restaurant.getId());
//		parameterMap.put("restaurantId", "53392e725e3942cbb7080f81e31b8e0d");
		return restaurantOrderService.getCancelOrderTotalMoney(parameterMap);
	}



}
